﻿@model TagGroupModel
@{
    ViewBag.Title = Resources.EnetaMvc.PageTitle_AdminTagGroups;
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
}

<style>
h3 {
    font-size: 16px !important;
    font-weight: bold;
}
    ul {
        margin-left:0px;
        padding-left:0px;
    }
</style>
<div class="content-txt">
    <div class="blog-head">
        <h2>@Resources.EnetaMvc.PageTitle_AdminTagGroups</h2>
    </div>
    <div class="content-info-area">
        @{ Html.BeginForm("Create", "TagGroups", FormMethod.Post); }
            @Html.TextBoxFor(x=> Model.TagGroup.Name)
            <button type="submit">@Resources.EnetaMvc.Add</button>
        @{ Html.EndForm(); }

        <ul>
        @foreach (var tagGroup in Model.TagGroups)
        { 
            <li>
                <div class="editform" style="display:none;">
                @{ Html.BeginForm("Edit", "TagGroups", FormMethod.Post, new { style = "display:inline;" }); }
                    @Html.HiddenFor(x=> tagGroup.Id)
                    @Html.TextBoxFor(x => tagGroup.Name, new { style="width:150px;" })
                    <button type="submit">@Resources.EnetaMvc.Save</button>
                    <button class="taggroup-cancel-button">@Resources.EnetaMvc.Cancel</button>
                @{ Html.EndForm(); }
                </div>
                <div class="viewform" style="">
                    <span style="width:150px;display:inline-block;">@tagGroup.Name</span>
                    <button class="taggroup-edit-button">@Resources.EnetaMvc.Edit</button>
                    @{ Html.BeginForm("Delete", "TagGroups", FormMethod.Post, new { style="display:inline;" }); }
                        <input type="hidden" name="id" value="@tagGroup.Id" />
                        <button data-taggroupid="@tagGroup.Id" class="taggroup-delete-button">@Resources.EnetaMvc.Delete</button>
                    @{ Html.EndForm(); }
                </div>
            </li>
        }
        </ul>
    </div>
</div>


@section Scripts {
    <script type="text/javascript">
        $(function () {
            $(".taggroup-edit-button").click(function () {
                var viewform = $(this).closest(".viewform");
                var editform = viewform.parent().children().eq(0);
                viewform.css("display", "none");
                editform.css("display", "block");
                return false;
            });


            $(".taggroup-cancel-button").click(function () {
                var editform = $(this).closest(".editform"); 
                var viewform = editform.parent().children().eq(1);
                editform.css("display", "none");
                viewform.css("display", "block");
                return false;
            });


            $(".taggroup-delete-button").click(function () {
                var tagGroupId = $(this).attr("data-taggroupid");
                var actionUrl = "@Url.Action("HasTags")";
                var deleteForm = $(this).parent();
                
                $.ajax({
                    url: actionUrl,
                    data: { "id": tagGroupId },
                    dataType: "json",
                    success: function (data) {
                        console.log(data);
                        if (data == false)  //if it has no tags then delete group
                            deleteForm.submit();
                        else {                  //else ask for permision to delete
                            var confirmResult = confirm("@Resources.EnetaMvc.PageAdminTagGroups_DeleteMessage");
                            if (confirmResult == true)
                                deleteForm.submit();
                        }
                    }
                });
                return false;
            });
        });
    </script>
}
